package com.binaryTree;

import java.util.LinkedList;
import java.util.Queue;

public class MinimumDepthofBinaryTree0228_111 {
    class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
   }
    public int minDepth(TreeNode root) {
        Queue<TreeNode> que = new LinkedList<>();
        int res = 0;
        if(root != null) que.offer(root);
        else return res;

        while (!que.isEmpty()){
            int len = que.size();

            while (len>0){
                TreeNode node = que.poll();
                if(node.left != null) que.offer(node.left);
                if(node.right != null) que.offer(node.right);

                if(node.left == null && node.right == null) return ++res;
                len--;
            }
            res++;
        }
        return res;
    }
}
